I’ve made this notes while reading Advanced Linux Programming
man -k keyword
C-x C-fC-x C-sC-x C-c(global-font-lock-mode t) M-x gdbgcc -c -I-D NAME=value-O2-L-l-o ex.name files.o-g target: depends on command clean: rm -f *.o ex.name
where up nprint variablebreak funcnamerun argsnext, stepThe strace command traces the execution of another program, listing any system calls the program makes and any signals it receives.
If you invoke GCC with -Wall and -pedantic, the compiler issues warnings about risky or possibly erroneous programming constructions.
The first step in profiling a program is to annotate its executable to collect profiling information. To do so, use the -pg compiler flag when both compiling the object files and linking. Then use gprof to display profiling results
GNU Coding Standards on User Interfaces info “(standards)User Interfaces”
parse command line options: getopt_long
create temp file: mkstemp (sytem call) next call unlink, the name of the file will disappear, but the content will stay until file closed, so there is no need to delete file manually
Create temp file: tmpfile (stdlib)
% ar cr libtest.a test1.o test2.o
The cr flags tell ar to create the archive.
Linking into application: % gcc -static -o app app.o -L. –ltest
% gcc -c -fPIC test1.c
The -fPIC option tells the compiler that you are going to be using test.o as part of a shared object.
Then you combine the object files into a shared library, like this: % gcc -shared -fPIC -o libtest.so test1.o
Linking is the same way.
The ldd command displays the shared libraries that are linked into an executable.
dlopen (”libtest.so”, RTLD_LAZY)
To use dynamic loading functions, include the <dlfcn.h> header file and link with the –ldl option to pick up the libdl library.
void* handle = dlopen (”libtest.so”, RTLD_LAZY); void (*test)() = dlsym (handle, “my_function”); (*test)(); dlclose (handle);
getpid() returns pid
getppid() returns parent pid
List of processes in brief form
% ps -e -o pid,ppid,command
-f (full listing), -l (long listing), or -j (jobs listing)
priority
% nice -n 10 sort input.txt > output.txt
You can use the renice command to change the niceness of a running process from the command line.
ulimit shell command enables you to restrict the resource usage of programs you run
The ipcs command provides information on interprocess communication facilities, including shared segments. Use the -m flag to obtain information about shared memory.
If this memory segment was erroneously left behind by a program, you can use the ipcrm command to remove it.
For example, to redirect a process’s standard input to a file descriptor fd, use this line: dup2 (fd, STDIN_FILENO);
The call to popen creates a child process executing the sort command, replacing calls to pipe, fork, dup2, and execlp.
% mkfifo /tmp/fifo
The first character of the output from ls is p, indicating that this file is actually a FIFO (named pipe). In one window, read from the FIFO by invoking the following:
% cat < /tmp/fifo
write:
% cat > /tmp/fifo
Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts. The PTY device acts like a terminal device—it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts. You can display the terminal device associated with a process using the ps command. Specify tty as one of the fields of a custom format with the -o option.
General information % man 5 proc
chroot
The root directory for a process can be changed using the chroot call or the chrootcommand. [2]
cpuid
See the documentation of the cpuid instruction in Intel’s IA-32 Intel Architecture Software Developer’s Manual, Volume 2: Instruction Set Reference. This manual is available from http://developer.intel.com/design To read more about the x86 instruction set, which we will use in this chapter, see http://developer.intel.com/design/pentiumii/manuals/ and http://www.x86-64.org/documentation.
hostname
The /proc/sys/kernel/hostname and /proc/sys/kernel/domainname entries contain the computer’s hostname and domain name, respectively. This information is the same as that returned by the uname system call.
Disc usage info: % df -h /tmp/virtual-fs Filesystem Size Used Avail Use% Mounted on /tmp/disk-image 9.7M 13k 9.
use assert liberally throughout your programs. gcc option to exclude asserts in release: -DNDEBUG
use htons to convert the port number to network byte order
A listing of system calls for your version of the Linux kernel is in /usr/include/asm/unistd.h.
gettimeofday (&tv, &tz); Returns time
The mlock family of system calls allows a program to lock some or all of its address space into physical memory. This prevents Linux from paging this memory to swap space, even if the program hasn’t accessed it for a while. Works only for root proccesses.
Sendfile The sendfile system call provides an efficient mechanism for copying data from one file descriptor to another. The file descriptors may be open to disk files, sockets, or other devices.